/*
 * Copyright 2010 Biz-e (info@biz-e.nl)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.biz_e.acl.server.dao;

import nl.biz_e.acl.shared.model.Context;
import nl.biz_e.appengine.lib.server.objectify.AbstractBaseDao;
import nl.biz_e.appengine.lib.server.objectify.ObjectifyUtils;

import com.googlecode.objectify.ObjectifyService;

/**
 * DAO for accessing and manipulating {@link Context} entities
 * 
 * @author Joost A. Bloemsma
 * 
 */
public class ContextDao extends AbstractBaseDao<Context> {
    static {
        ObjectifyService.register(Context.class);
    }

    /**
     * Constructs a new ContextDao instance
     */
    public ContextDao() {
        super(Context.class);
    }

    /**
     * @param ctx
     * @return The parent context as an entity
     */
    public Context getParentContext(Context ctx) {
        return get(ctx.getParentContext());
    }

    /**
     * Returns a context based on the ContextId
     * 
     * @param contextId
     * @return INstance of {@link Context}
     */
    public Context getContext(String contextId) {
        return ObjectifyUtils.getResultSingle(ofy().query(Context.class)
                .filter("contextId", contextId));
    }

}
